home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * ITex Pierre Baillargeon 1989/ Michel Laliberté, mai 1991 *
- * cc +L <source>.c *
- * ln +Cdb <source>.o -lc32 *
- ************************************************************************/
- #include <exec/types.h>
- #include <exec/io.h>
- #include <exec/memory.h>
- #include <libraries/dos.h>
- #include <libraries/dosextens.h>
- #include <graphics/gfxbase.h>
- #include <intuition/intuition.h>
- #include <intuition/preferences.h>
- #include <workbench/startup.h>
- #include <workbench/workbench.h>
- #include <workbench/icon.h>
- #include <functions.h>
-
- struct WBStartup *WBenchMsg;
- struct DiskObject *diskobj;
- struct WBArg *wbarg;
-
- static WORD bordbox[] = {8,2,8,4,0,4,0,0,8,0,8,2,20,2,20,8,4,8,4,4};
- static struct Border iconbord = { /* Icon border */
- 1, 0, 1, 0, JAM1,10,bordbox,NULL,
- };
-
- struct Gadget IconGadget = {
- NULL,5,NULL,22,10,NULL,
- RELVERIFY+GADGIMMEDIATE+GADGHCOMP,
- BOOLGADGET,(APTR)&iconbord,
- NULL,NULL,NULL,NULL,8,NULL
- };
-
- struct NewWindow NW = {
- 0,0,0,0,0,1,
- MOUSEBUTTONS | RAWKEY | NEWSIZE | CLOSEWINDOW | GADGETUP,
- SMART_REFRESH | NOCAREREFRESH | ACTIVATE | RMBTRAP |
- WINDOWSIZING | SIZEBBOTTOM | WINDOWDRAG | WINDOWCLOSE | WINDOWDEPTH,
- &IconGadget,0L,0L,0L,0L,
- 50,50,-1,-1,WBENCHSCREEN };
-
- struct IntuiText Mess = {
- 2,0,JAM2,0,0,NULL,
- (UBYTE *)" Use Mouse or Up/Down Cursors »*« FAST: Shift-Cursor »*« [ESC] to QUIT",
- NULL
- };
-
- struct Preferences prefs;
-
- WORD X, Y, k,fr;
- LONG i, j;
- char fname[31],temp[95];
- BYTE *Buffer, **Memory;
- struct Screen *Screen;
- struct Window *Window;
- struct RastPort *RastPort;
- struct IntuiMessage *Msg;
- LONG StartTopLine, CurrentTopLine, Len, Number = 1L, MaxLine, MaxColumn;
- struct IntuitionBase *IntuitionBase;
- struct IconBase *IconBase;
- struct GfxBase *GfxBase;
-
- unsigned char fvar[4][10] = {"File: ","Line: ","Fichier: ","Ligne: "};
-
- _abort(){}
-
- main (argc,argv)
- WORD argc;
- char *argv[];
- {
- struct FileHandle *File;
- APTR Object;
- ULONG Class;
- UWORD Code, Qualifier, iconx = 560, icony = 0;
- BOOL chg=TRUE;
- char **toolptr, **findtools();
-
-
- IntuitionBase = (struct IntuitionBase *)
- OpenLibrary("intuition.library", 0L);
- if (IntuitionBase == NULL) {
- exit(1);
- }
- if ((GfxBase = (void *)OpenLibrary("graphics.library",0)) == NULL) {
- exit();
- }
- IconBase = (struct IconBase *)OpenLibrary("icon.library",0L);
- if ((IconBase) == NULL )
- exit(1);
- if (argc) { /* Lancé du CLI */
- toolptr = findtools(argv[0],((struct Process *)
- FindTask(NULL))->pr_CurrentDir);
- if ( !(File = Open(argv[1],MODE_OLDFILE))) {
- exit (1);
- }
- strcat(fname,argv[1]);
- }
- else { /* Open the file - Workbench */
- if (WBenchMsg->sm_NumArgs == 1) exit (1);
- wbarg = &(WBenchMsg->sm_ArgList[1]);
- CurrentDir(wbarg->wa_Lock);
- if ( !(File = Open(wbarg->wa_Name,MODE_OLDFILE))) {
- exit (1);
- }
- strcat(fname,wbarg->wa_Name);
- wbarg = &(WBenchMsg->sm_ArgList[WBenchMsg->sm_NumArgs-1]);
- toolptr = findtools(wbarg->wa_Name,wbarg->wa_Lock);
- }
- if ((FindToolType(toolptr, "FR"))[0] == '1') {
- fr = 2;
- Mess.IText = (UBYTE *)" HAUT/BAS: Souris ou curseurs »*« PAGE: [Shift] Curseur »*« FIN: [ESC]";
- }
- NW.Height = GfxBase->NormalDisplayRows; /* Open the Window. */
- NW.Width = GfxBase->NormalDisplayColumns;
- GetPrefs(&prefs,sizeof(struct Preferences));
- if (prefs.LaceWB) NW.Height += NW.Height;
- MaxColumn = (NW.Width - 6) / 8;
- MaxLine = (NW.Height - 22) / 8;
- NW.Title = (UBYTE *) fname;
- if( !(Window = OpenWindow(&NW))) {
- CleanUp();
- }
- RastPort = Window->RPort;
- PrintIText(RastPort, &Mess, 10, NW.Height-10);
- SetAPen (RastPort, 0);
- RectFill (RastPort,2,Window->Height-10,28,Window->Height-2);
- SetAPen (RastPort, 1);
- IconGadget.TopEdge = Window->Height-10;
- RefreshGList(&IconGadget,Window,NULL,1);
-
- Len = Seek (File, 0L, OFFSET_END); /* Find its lenght. */
- Len = Seek (File, 0L, OFFSET_BEGINNING);
- if ( !(Buffer = (BYTE *)AllocMem(Len + 1, MEMF_CLEAR))) {
- Close (File); /* Alloc the memory needed.*/
- CleanUp();
- }
- Buffer[Len] = 10;
- Read (File, Buffer, Len); /* Read and Close it. */
- Close (File);
- for (i = 0; i < Len; i += 1L){ /* Find Number of lines. */
- if (Buffer[i] == 10)
- Number += 1L;
- }
- if ( !(Memory = (BYTE **)AllocMem(Number * 4L, MEMF_CLEAR))) {
- CleanUp(); /* Alloc memory to hold pointers to the line. */
- }
- i = j = 0; /* Find all lines' beginning. */
- Memory[j++] = Buffer;
- while(i < Len) {
- if(Buffer[i] == 10) {/*** Check for Line-Feed ***/
- Memory[j++] = &Buffer[i + 1];
- }
- i++;
- }
-
- CurrentTopLine = 0L;
-
- for ( j = 0L; j < Number && j < MaxLine; j++) {
- WriteLine(j, (LONG)(j * 8 + 17)); /*Fill the screen with text.*/
- }
- for(;;) { /* Now wait forever (kinda) for user input. */
- if (chg) {
- sprintf(temp,"ITex v1.0 %s%s %s %d / %d ",fvar[0+fr],fname,
- fvar[1+fr],CurrentTopLine+MaxLine,Number);
- SetWindowTitles(Window,temp,-1);
- chg = FALSE;
- }
- WaitPort(Window->UserPort);
- while ((Msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) != 0L)
- {
- Class = Msg->Class;
- Code = Msg->Code;
- Object = Msg->IAddress;
- Qualifier = Msg->Qualifier;
- X = Msg->MouseX;
- Y = Msg->MouseY;
- ReplyMsg(Msg);
- if (Class == MOUSEBUTTONS && Code == SELECTDOWN) {
- chg = TRUE;
- if( Y > Window->Height / 2) {
- ScrollUp();
- }
- else ScrollDown();
- }
- else if(Class == GADGETUP && Object == (APTR)&IconGadget) {
- NW.LeftEdge = Window->LeftEdge;
- NW.TopEdge = Window->TopEdge;
- NW.Width = Window->Width;
- NW.Height = Window->Height;
- CloseWindow(Window);
- Window = NULL;
- iconify (&iconx, &icony, 24, 10, NULL, &iconbord, 1);
- if( !(Window = OpenWindow(&NW))) {
- CleanUp();
- }
- else {
- RastPort = Window->RPort;
- ReFill();
- }
- }
- else if(Class == RAWKEY) {
- chg = TRUE;
- if(Code == 0x45)
- CleanUp();
- if(Code == 0x4D) {
- if(Qualifier == 0x8001 || Qualifier == 0x8002) {
- if(CurrentTopLine+MaxLine-1 < Number - MaxLine)
- CurrentTopLine += MaxLine -1;
- else
- CurrentTopLine = Number - MaxLine;
- SetAPen (RastPort, 0);
- RectFill (RastPort,3,10,Window->Width-7,Window->Height-14);
- SetAPen (RastPort, 1);
- for ( j = 0; j < Number && j < MaxLine; j++)
- WriteLine(CurrentTopLine+j, (LONG) j*8 + 17);
- }
- else {
- ScrollUp();
- continue;
- }
- }
- if (Code == 0x4C) {
- if(Qualifier == 0x8001 || Qualifier == 0x8002) {
- if (CurrentTopLine-MaxLine-1 >0 )
- CurrentTopLine -= MaxLine -1;
- else
- CurrentTopLine = 0;
- SetAPen (RastPort, 0);
- RectFill (RastPort,3,10,Window->Width-7,Window->Height-14);
- SetAPen (RastPort, 1);
- for ( j = 0; j < Number && j < MaxLine; j++)
- WriteLine(CurrentTopLine+j, (LONG) j*8 + 17);
- }
- else {
- ScrollDown();
- continue;
- }
- }
- }
- else if(Class == NEWSIZE) {
- MaxColumn = (Window->Width - 6) / 8;
- MaxLine = (Window->Height - 22) / 8;
- ReFill();
- }
- else if(Class == CLOSEWINDOW)
- CleanUp();
- }
- }
- }
-
- char **findtools(name,lock) /* Lecture du Tool Type */
- char *name;
- BPTR lock;
- {
- BPTR olddir;
- char **tools = NULL;
-
- if(lock == NULL) return (NULL);
- olddir = (BPTR)CurrentDir(lock);
- if((diskobj=GetDiskObject(name)) != NULL)
- tools=diskobj->do_ToolTypes;
- return(tools);
- }
-
- ScrollUp() /* Scroll BitMap upward. */
- {
- StartTopLine=CurrentTopLine;
- while (1) {
- if ((Msg = (struct IntuiMessage *) GetMsg(Window->UserPort)) != 0L) {
- if (Msg->Code & 0x80) {
- ReplyMsg (Msg);
- return;
- }
- else {
- if(Msg->Class == NEWSIZE) {
- MaxColumn = (Window->Width - 6) / 8;
- MaxLine = (Window->Height - 22) / 8;
- ReFill();
- }
- else {
- if(Msg->Class == CLOSEWINDOW) {
- ReplyMsg (Msg);
- CleanUp();
- }
- }
- }
- ReplyMsg (Msg);
- }
- if (CurrentTopLine < Number - MaxLine) {
- CurrentTopLine += 1L;
- }
- else return;
-
- ClipBlit(RastPort,3L,19L,RastPort,3L,11L,(LONG)(Window->Width-6),(LONG)(((Window->Height-22)&0xfff8)-8),0xC0L);
- WriteLine(CurrentTopLine + MaxLine - 1L, (LONG)(MaxLine * 8 + 9));
- }
- }
-
- ScrollDown() /* Scroll BitMap downward.*/
- {
- StartTopLine=CurrentTopLine;
- while (1) {
- if ((Msg = (struct IntuiMessage *)GetMsg(Window->UserPort)) != 0L) {
- if (Msg->Code & 0x80) {
- ReplyMsg (Msg);
- return;
- }
- else {
- if(Msg->Class == NEWSIZE) {
- MaxColumn = (Window->Width - 6) / 8;
- MaxLine = (Window->Height - 22) / 8;
- ReFill();
- }
- else {
- if(Msg->Class == CLOSEWINDOW) {
- ReplyMsg (Msg);
- CleanUp();
- }
- }
- }
- ReplyMsg (Msg);
- }
- if (CurrentTopLine != 0) {
- CurrentTopLine--;
- }
- else return;
-
- ClipBlit(RastPort,3L,11L,RastPort,3L,19L,(LONG)(Window->Width-6),(LONG)(((Window->Height-22)&0xfff8)-8),0xC0L);
- WriteLine (CurrentTopLine, 17L);
- }
- }
-
- WriteLine(Line, Y) /*This is the routine that draw the text correctly.*/
- LONG Line, Y;
- {
- WORD i, j, k;
- BYTE LineBuffer[127];
-
- Move(RastPort, 3L, Y);
- SetAPen(RastPort, 1);
- SetBPen(RastPort, 0);
-
- for ( i = 0, j = 0, k = 0; k < MaxColumn; i++) {
- if ( Memory[Line][i] == 10) { /*** Check for LF ***/
- Text (RastPort, LineBuffer, (LONG)j);
- SetAPen (RastPort, 0);
- RectFill (RastPort, (LONG)(k * 8 + 3), Y - 6L, (LONG)(Window->Width - 3), Y + 1L);
- SetAPen (RastPort, 1);
- return;
- }
- else {
- if (Memory[Line][i] == 9) { /*** Check for Tab ***/
- do {
- if (k < MaxColumn) {
- LineBuffer[j++] = 32;
- k++;
- }
- else {
- Text(RastPort, LineBuffer, (LONG)j);
- return;
- }
- }
- while (k % 8 != 0);
- }
- else {
- LineBuffer[j++] = Memory[Line][i];
- k++;
- }
- } /*** Close else of if LF ***/
- } /*** Close line writing loop ***/
- Text (RastPort, LineBuffer, (LONG)j);
- }
-
- ReFill() /* Refill window with text when resized.*/
- {
- register LONG j, i;
- for ( j= CurrentTopLine, i=0 ; j < Number && j < CurrentTopLine+MaxLine; j++,i++) {
- WriteLine(j, (LONG)(i * 8 + 17));
- }
- PrintIText(RastPort, &Mess, 10, Window->Height-10);
- SetAPen (RastPort, 0);
- RectFill (RastPort,2,Window->Height-10,28,Window->Height-2);
- SetAPen (RastPort, 1);
- IconGadget.TopEdge = Window->Height-10;
- RefreshGList(&IconGadget,Window,NULL,1);
- }
-
- CleanUp() /* Release everything. */
- {
- if (diskobj) FreeDiskObject(diskobj);
- if (Memory) FreeMem (Memory,Number*4L);
- if (Buffer) FreeMem (Buffer,Len+1);
- if (Window) CloseWindow (Window);
- if (IconBase) CloseLibrary(IconBase);
- if (GfxBase ) CloseLibrary(GfxBase);
- if (IntuitionBase) CloseLibrary(IntuitionBase);
- exit (0);
- }
-